// pages/add_pinglun/add_pinglun.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel:"",
    pinglun:[],
    obj:[],
    content:"",  // 评语
    to_tel:"",  // 被回复人手机号
    to_nickName:"",  // 被回复人昵称
    msg:"请输入...", // 回复人的提示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var data = JSON.parse(options.data)
    console.log(data);
    this.setData({
      obj:data
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPinglun();
    this.setData({
      tel:wx.getStorageSync('tel')
    });
  },

  // 评论内容
  getInput(e){
    console.log(e.detail.value);
    this.setData({
      content:e.detail.value
    });
  },

  addPinglun(){
    if(!wx.getStorageSync('tel')){
      wx.showModal({
        title: '提示',
        content: "还没有登录！",
        showCancel:false,
        success () {
          return
        }
      })
    }else{
      this.add();
    }
  },

  // 发表评论
  add(){
    if(!this.data.content){
      wx.showModal({
        msg: '提示',
        content: "请输入评论内容！",
        showCancel:false,
        success:()=> {
        }
      })
    }else{
      wx.showLoading({
        msg: '提交中',
      })
      wx.request({
        url: 'http://127.0.0.1:5000/addPinglun', // 发布论坛
        data: {
          from_tel: wx.getStorageSync("tel"), // 评论人手机号
          content:this.data.content,  // 评论内容
          to_tel:this.data.to_tel,    // 被回复人手机号
          luntan_id:this.data.obj.luntan_id,   // 论坛id
          from_nickName:wx.getStorageSync("userInfo").nickName,  // 评论人昵称
          from_img:wx.getStorageSync("userInfo").avatarUrl,  // 评论人头像
          to_nickName:this.data.to_nickName,  // 被回复人昵称 
          shijian:this.getTime(),  // 评论时间
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success:res=>{
          wx.hideLoading()
          this.setData({
            content:"",
            msg:"请输入...",
            to_tel:"",
            to_nickName:"",
          });
          wx.showModal({
            msg: '提示',
            content: "评论成功！",
            showCancel:false,
            success:()=> {
              this.getPinglun();
            }
          })
        }
      })
    }
  },

  // 回复
  huifu(e){
    console.log(e.currentTarget.dataset.item);
    this.setData({
      to_tel:e.currentTarget.dataset.item.from_tel,
      to_nickName:e.currentTarget.dataset.item.from_nickName,
      msg:"回复"+e.currentTarget.dataset.item.from_nickName
    });
  },

       // 获取论坛的评论信息
       getPinglun(){
        wx.showLoading({
          title: '加载中...',
        })
        wx.request({
          url: 'http://127.0.0.1:5000/getLTPinglun',
          data: {
            luntan_id:this.data.obj.luntan_id
          },
          header: {
            'content-type': 'application/json' 
          },
          success:res=> {
            console.log(res.data.list)
            this.setData({
              pinglun:res.data.list
            });
            wx.hideLoading()
          }
        })
      },

   // 删除
   del(e){
    console.log(e.currentTarget.dataset.id);
    wx.showModal({
      title: '提示',
      content: '确认要删除吗？',
      success:res=>{
        if (res.confirm) {
          wx.request({
            url: 'http://127.0.0.1:5000/delLuntan',
            data: {
              luntan_id:e.currentTarget.dataset.id
            },
            header: {
              'content-type': 'application/json' 
            },
            success:res=> {
              console.log(res.data)
              this.delPinglun(e.currentTarget.dataset.id)
              wx.showModal({
                msg: '提示',
                content: "删除成功！",
                showCancel:false,
                success:()=> {
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
         
        }
      }
    })
  },

  // 论坛删除后，删除对应的所有评论
  delPinglun(id){
    wx.request({
      url: 'http://127.0.0.1:5000/delPinglun',
      data: {
        luntan_id:id
      },
      header: {
        'content-type': 'application/json' 
      },
      success:res=> {
        console.log(res.data)
      }
    })
  },


    // 获取当前时间
  //显示日期在页面上  yyy-MM-dd
  getTime(){
	  var now = new Date();
	  var year = now.getFullYear(); //得到年份
	  var month = now.getMonth();//得到月份
	  var date = now.getDate();//得到日期
	  var day = now.getDay();//得到周几
	  var hour = now.getHours();//得到小时
	  var minu = now.getMinutes();//得到分钟
	  var sec = now.getSeconds();//得到秒
	  month = month + 1;
	  if (month < 10) month = "0" + month;
	  if (date < 10) date = "0" + date;
	  if (hour < 10) hour = "0" + hour;
	  if (minu < 10) minu = "0" + minu;
	  if (sec < 10) sec = "0" + sec;
	  var time = "";
	  //精确到分
    time = year + "-" + month + "-" + date+ " " + hour + ":" + minu;
    // return time;
    console.log(time)
    return time
},
})